package com.gee.spring.cloud.data.transform.core.util;

import com.gee.spring.cloud.common.core.entity.center.config.*;
import com.gee.spring.cloud.common.starter.util.spring.AppContextUtil;
import com.gee.spring.cloud.data.transform.core.center.config.handler.*;

/**
 * @author gepengjun
 * @since 2024/4/2 8:40
 */

public class MappingUtil {

    public static long getMappingDevIdentity(long orgId, long devIdentity){
        Device device = AppContextUtil.getBean(DeviceHandler.class).getByDevIdentity(devIdentity);
        if (device == null){
            return devIdentity;
        }
        DeviceMapping deviceMapping = AppContextUtil.getBean(DeviceMappingHandler.class).getByOrgIdAndMappingDevId(orgId, device.getId());
        if (deviceMapping == null){
            return devIdentity;
        }
        return deviceMapping.getMappingDevIdentity();
    }

    public static String getMappingDevCode(long orgId, String devCode){
        Device device = AppContextUtil.getBean(DeviceHandler.class).getByDevCode(devCode);
        if (device == null){
            return devCode;
        }
        DeviceMapping deviceMapping = AppContextUtil.getBean(DeviceMappingHandler.class).getByOrgIdAndMappingDevId(orgId, device.getId());
        if (deviceMapping == null){
            return devCode;
        }
        return deviceMapping.getMappingDevCode();
    }

    public static String getMappingDevName(long orgId, String devName){
        Device device = AppContextUtil.getBean(DeviceHandler.class).getByDevName(devName);
        if (device == null){
            return devName;
        }
        DeviceMapping deviceMapping = AppContextUtil.getBean(DeviceMappingHandler.class).getByOrgIdAndMappingDevId(orgId, device.getId());
        if (deviceMapping == null){
            return devName;
        }
        return deviceMapping.getMappingDevName();
    }

    public static long getMappingSateIdentity(long orgId, long sateIdentity){
        Satellite satellite = AppContextUtil.getBean(SatelliteHandler.class).getBySateIdentity(sateIdentity);
        if (satellite == null){
            return sateIdentity;
        }
        SatelliteMapping satelliteMapping = AppContextUtil.getBean(SatelliteMappingHandler.class).getByOrgIdAndMappingSateId(orgId, satellite.getId());
        if (satelliteMapping == null){
            return sateIdentity;
        }
        return satelliteMapping.getMappingSateIdentity();
    }

    public static long getMappingBid(long orgId, long bid){
        Frame frame = AppContextUtil.getBean(FrameHandler.class).getByBid(bid);
        if (frame == null){
            return bid;
        }
        FrameMapping frameMapping = AppContextUtil.getBean(FrameMappingHandler.class).getByOrgIdAndMappingFrameId(orgId, frame.getId());
        if (frameMapping == null){
            return bid;
        }
        return frameMapping.getMappingFrameBid();
    }

    public static long getMappingOrgCenterIdentity(long orgId, long centerIdentity){
        OrgCenter orgCenter = AppContextUtil.getBean(OrgCenterHandler.class).getByCenterIdentity(centerIdentity);
        if (orgCenter == null){
            return centerIdentity;
        }
        OrgCenterMapping orgCenterMapping = AppContextUtil.getBean(OrgCenterMappingHandler.class).getByOrgIdAndOrgCenterId(orgId, orgCenter.getOrgId());
        if (orgCenterMapping == null){
            return centerIdentity;
        }
        return orgCenterMapping.getMappingCenterIdentity();
    }

    public static long getSateIdentity(long orgId, long mappingSateIdentity){
        SatelliteMapping satelliteMapping = AppContextUtil.getBean(SatelliteMappingHandler.class).getByOrgIdAndMappingSateIdentity(orgId, mappingSateIdentity);
        if (satelliteMapping == null){
            return mappingSateIdentity;
        }
        Satellite satellite = AppContextUtil.getBean(SatelliteHandler.class).getById(satelliteMapping.getSateId());
        if (satellite == null){
            return mappingSateIdentity;
        }
        return satellite.getSateIdentity();
    }

    public static long getOrgCenterIdentity(long orgId, long mappingCenterIdentity){
        OrgCenterMapping orgCenterMapping = AppContextUtil.getBean(OrgCenterMappingHandler.class).getByOrgIdAndMappingCenterIdentity(orgId, mappingCenterIdentity);
        if (orgCenterMapping == null){
            return mappingCenterIdentity;
        }
        OrgCenter orgCenter = AppContextUtil.getBean(OrgCenterHandler.class).getByOrgCenterId(orgCenterMapping.getOrgCenterId());
        if (orgCenter == null){
            return mappingCenterIdentity;
        }
        return orgCenter.getCenterIdentity();
    }

    public static long getFrameBid(long orgId, long mappingFrameBid){
        FrameMapping frameMapping = AppContextUtil.getBean(FrameMappingHandler.class).getByOrgIdAndMappingFrameBid(orgId, mappingFrameBid);
        if (frameMapping == null){
            return mappingFrameBid;
        }
        Frame frame = AppContextUtil.getBean(FrameHandler.class).getById(frameMapping.getFrameId());
        if (frame == null){
            return mappingFrameBid;
        }
        return frame.getBid();
    }

    public static long getDevIdentity(long orgId, long mappingDevIdentity){
        DeviceMapping deviceMapping = AppContextUtil.getBean(DeviceMappingHandler.class).getByOrgIdAndMappingDevIdentity(orgId, mappingDevIdentity);
        if (deviceMapping == null){
            return mappingDevIdentity;
        }
        Device device = AppContextUtil.getBean(DeviceHandler.class).getById(deviceMapping.getDevId());
        if (device == null){
            return mappingDevIdentity;
        }
        return device.getDevIdentity();
    }

    public static String getDevCode(long orgId, String mappingDevCode){
        DeviceMapping deviceMapping = AppContextUtil.getBean(DeviceMappingHandler.class).getByOrgIdAndDevCode(orgId, mappingDevCode);
        if (deviceMapping == null){
            return mappingDevCode;
        }
        Device device = AppContextUtil.getBean(DeviceHandler.class).getById(deviceMapping.getDevId());
        if (device == null){
            return mappingDevCode;
        }
        return device.getDevCode();
    }

    public static String getDevName(long orgId, String mappingDevName){
        DeviceMapping deviceMapping = AppContextUtil.getBean(DeviceMappingHandler.class).getByOrgIdAndDevName(orgId, mappingDevName);
        if (deviceMapping == null){
            return mappingDevName;
        }
        Device device = AppContextUtil.getBean(DeviceHandler.class).getById(deviceMapping.getDevId());
        if (device == null){
            return mappingDevName;
        }
        return device.getDevName();
    }
}
